package com.mall.service.homepage.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeBrand;
import com.mall.model.domain.SmsHomeNewProduct;
import com.mall.service.homepage.service.HomeNewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class HomeNewProductController {

    @Autowired
    private HomeNewProductService homeNewProductService;


    /**
     * 添加首页新品
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsHomeNewProduct> smsHomeNewProducts){
        homeNewProductService.saveBatch(smsHomeNewProducts);
        return CommonResult.success("添加成功");
    }
    /**
     * 批量删除首页新品
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Long> ids){
        homeNewProductService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }
    /**
     * 分页查询首页新品
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<SmsHomeNewProduct>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                       @RequestParam(defaultValue = "10") Integer pageSize, String productName, Integer recommendStatus){
        CommonPage<SmsHomeNewProduct> page =homeNewProductService.findByPage(pageNum,pageSize,productName,recommendStatus);

        return CommonResult.success(page);
    }
    /**
     * 批量修改推荐品牌状态
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam List<Long> ids ,Integer recommendStatus){
        LambdaQueryWrapper<SmsHomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SmsHomeNewProduct::getId,ids);
        List<SmsHomeNewProduct> list = homeNewProductService.list(queryWrapper);
        ArrayList<SmsHomeNewProduct> objects = new ArrayList<>();
        for (SmsHomeNewProduct smsHomeNewProduct : list) {

            smsHomeNewProduct.setRecommendStatus(recommendStatus);

            objects.add(smsHomeNewProduct);
        }

        homeNewProductService.updateBatchById(objects);
        return CommonResult.success("批量修改成功");
    }
    /**
     * 修改推荐品牌排序
     */
    @PostMapping("/update/sort/{id}")
    public CommonResult updateSort(@PathVariable Long id,Integer sort){
        SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
        smsHomeNewProduct.setId(id);
        smsHomeNewProduct.setSort(sort);
        homeNewProductService.updateById(smsHomeNewProduct);
        return CommonResult.success("修改成功");
    }






}